package ui.fragment.group_management;

import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.trek2000.android.enterprise.Enterprise;
import com.trek2000.android.enterprise.R;

import java.util.ArrayList;

import adapter.group_management.MemberTabInGroupFeedFragmentAdapter;
import controller.company.SelectCompanyAsync;
import controller.group_management.GroupManagementAsync;
import controller.group_management.MemberTabInGroupFeedFragmentAsync;
import model.AllCompanyFeedItem;
import ui.activity.group_management.GroupFeed;
import ui.activity.login.Login;

public class MemberTabInGroupFeedFragment extends Fragment
        implements View.OnClickListener{

    /**
     * String section
     */

    /**
     * View section
     */
    public static ListView mLvInMemberTabInGroupFeedFragment;
    public static MemberTabInGroupFeedFragmentAdapter memberTabInGroupFeedFragmentAdapter;
    /**
     * Others section
     */

    /**
     * Listener section
     */

    public static Fragment newInstance() {

        MemberTabInGroupFeedFragment fragment = new MemberTabInGroupFeedFragment();
        return fragment;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = getLayoutInflater(savedInstanceState).inflate(
                R.layout.fragment_member_tab_in_group_feed_fragment, container, false);
        // Initial views
        initialViews(v);
        initialData();
        setDataForMemberTabInGroupFeedFragment();
        return v;
    }

    /**
     * Initialize methods
     */
    private void initialData() {
        if (MemberTabInGroupFeedFragmentAsync.mAlMemberTabInGroupFeed.size() > 0) {
            memberTabInGroupFeedFragmentAdapter = new MemberTabInGroupFeedFragmentAdapter(getActivity(),
                    R.layout.simple_list_item_in_member_tab_in_group_feed_fragment,
                    MemberTabInGroupFeedFragmentAsync.mAlMemberTabInGroupFeed.get(0).getmAlUsersOfMemberTabInGroupFeedItem());
            mLvInMemberTabInGroupFeedFragment.setAdapter(memberTabInGroupFeedFragmentAdapter);
        }
    }

    private void initialViews(View v) {
        mLvInMemberTabInGroupFeedFragment = (ListView) v.findViewById(R.id.lv_in_member_tab_in_group_feed_fragment);
    }

    /**
     * The others methods
     */
    private void setDataForMemberTabInGroupFeedFragment() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
            new MemberTabInGroupFeedFragmentAsync(getActivity()).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,
                    String.valueOf(Login.user.getUserInfo().getUserGlobalId()),
                    String.valueOf(Login.user.getUserInfo().getAccessToken()),
                    String.valueOf(
                            Enterprise.company.getCompaniesOfUserInfoSelected().getUserCompanyId()),
                    String.valueOf(
                            Enterprise.company.getCompaniesOfUserInfoSelected().getCompanyId()),
                    String.valueOf(Enterprise.groupManagement.getGroupIdSelected()),
                    "get_members");
        else
            new MemberTabInGroupFeedFragmentAsync(getActivity()).execute(
                    String.valueOf(Login.user.getUserInfo().getUserGlobalId()),
                    String.valueOf(Login.user.getUserInfo().getAccessToken()),
                    String.valueOf(
                            Enterprise.company.getCompaniesOfUserInfoSelected().getUserCompanyId()),
                    String.valueOf(
                            Enterprise.company.getCompaniesOfUserInfoSelected().getCompanyId()),
                    String.valueOf(Enterprise.groupManagement.getGroupIdSelected()),
                    "get_members");
    }

}
